﻿// ———————————————————————–
// <copyright file="AWSConstants.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// AWSConstants.cs - "Constants For AWS"
// Project: EDXLSharp_AWSRouter - ICNETSERVICES
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2013
// Author:      Brian Wilkins The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.Generic;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace ICNETServices
{
  /// <summary>
  /// Contains the constants used in various pats of IC.NET
  /// </summary>
  public class AWSConstants
  {
    /// <summary>
    /// Log4net logging object
    /// </summary>
    private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    /// <summary>
    /// The path used in posting a EDXL-DE to IC.NET
    /// </summary>
    public const string DEPostServiceNamespace = "http://icnet.mitre.org/PostDE";
    /// <summary>
    /// The base icnet global namespace for WCF services
    /// </summary>
    public const string ICNETNamespace = "http://icnet.mitre.org";
    public const string ICNETQualifiedName = "icnet";
    public const string ValueListService = "ValueListService.svc";
    private static AWSConstantsConfig config;

    #region Public Accessors

    /// <summary>
    /// Initializes static members of the ICNETSOAConstants class. 
    /// </summary>
    static AWSConstants()
    {
      LoadConstants();
    }

    /// <summary>
    /// Gets the service URL for SQS
    /// </summary>
    public static string SQSServiceURL
    {
      get { return config.SQSServiceURL; }
    }

    /// <summary>
    /// Gets the service URL for DynamoDB
    /// </summary>
    public static string DynamoServiceURL
    {
      get { return config.DynamoServiceURL; }
    }

    /// <summary>
    /// Gets the base URL for KML icons
    /// </summary>
    public static string KMLIconURL
    {
      get { return config.KMLIconURL; }
    }

    /// <summary>
    /// Gets the fully qualified DE Processing queue url
    /// </summary>
    public static string DEProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.DEProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified Content Processing queue url
    /// </summary>
    public static string ContentProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.ContentProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified ContentFeed Processing queue url
    /// </summary>
    public static string ContentFeedProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.ContentFeedProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified History Processing queue url
    /// </summary>
    public static string HistoryProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.HistoryProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified Rules Processing queue url
    /// </summary>
    public static string RulesProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.RulesProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified ValueLists Processing queue url
    /// </summary>
    public static string ValueListsProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.ValueListsProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified Delete Processing queue url
    /// </summary>
    public static string DeleteProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.DeleteProcessing.QueueName); }
    }

    /// <summary>
    /// Gets the fully qualified Federation Processing queue url
    /// </summary>
    public static string FederationProcessingQueueUrl
    {
      get { return string.Format("{0}/{1}/{2}", config.SQSServiceURL, config.AccountID, config.FederationProcessing.QueueName); }
    }

    /// <summary>
    /// Gets or sets the URL for the DEProcessing SQS Queue
    /// </summary>
    public static ProcessConfig DEProcessing
    {
      get { return config.DEProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the DEProcessing SQS Queue
    /// </summary>
    public static ProcessConfig ContentProcessing
    {
      get { return config.ContentProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the DEProcessing SQS Queue
    /// </summary>
    public static ProcessConfig ContentFeedProcessing
    {
      get { return config.ContentFeedProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the DEProcessing SQS Queue
    /// </summary>
    public static ProcessConfig HistoryProcessing
    {
      get { return config.HistoryProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the RulesProcessing SQS Queue
    /// </summary>
    public static ProcessConfig RulesProcessing
    {
      get { return config.RulesProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the ValueListsProcessing SQS Queue
    /// </summary>
    public static ProcessConfig ValueListsProcessing
    {
      get { return config.ValueListsProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the DeleteProcessing SQS Queue
    /// </summary>
    public static ProcessConfig DeleteProcessing
    {
      get { return config.DeleteProcessing; }
    }

    /// <summary>
    /// Gets or sets the URL for the FederationProcessing SQS Queue
    /// </summary>
    public static ProcessConfig FederationProcessing
    {
      get { return config.FederationProcessing; }
    }
    #endregion

    /// <summary>
    /// Initializes the constants.
    /// </summary>
    private static void LoadConstants()
    {
      XmlSerializer s = new XmlSerializer(typeof(AWSConstantsConfig));
      TextReader r = null;

      string icnetRoot = null;

      icnetRoot = System.Environment.GetEnvironmentVariable("ICNET_ROOT", EnvironmentVariableTarget.Machine);

      if (icnetRoot == null)
      {
        Log.Warn("Missing ICNET_ROOT environment variable, defaulting to C:\\ICNET");
        icnetRoot = @"C:\ICNET";
      }

      string fileName = icnetRoot + @"\Config\AWS_Constants.xml";

      if (File.Exists(fileName))
      {
        r = new StreamReader(fileName);
      }
      else
      {
        
        Log.ErrorFormat("No config file: {0} Make sure the ICNET_ROOT system environment variable is correct.", fileName);
        return;
      }

      try
      {
        config = (AWSConstantsConfig)s.Deserialize(r);
        Log.InfoFormat("Deserialized configs from {0}.", fileName);
      }
      catch (Exception e)
      {
        Log.Error("Config file unparsable: " + fileName, e);
        config = null;
      }
      finally
      {
        r.Close();
      }
    }
  }

  /// <summary>
  /// Configuration for the AWS Feed Constants located in ICNETSOA.
  /// </summary>
  [Serializable]
  public class AWSConstantsConfig
  {
    /// <summary>
    /// Gets or sets the AWS account id
    /// </summary>
    [XmlElement("AccountID")]
    public string AccountID
    {
      get;
      set;
    }
    /// <summary>
    /// Gets or sets the AWS SQS service url
    /// </summary>
    [XmlElement("SQSServiceURL")]
    public string SQSServiceURL
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the AWS Dynamo service url
    /// </summary>
    [XmlElement("DynamoServiceURL")]
    public string DynamoServiceURL
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the base URL for KML icons
    /// </summary>
    [XmlElement("KMLIconURL")]
    public string KMLIconURL
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the DEProcessing configuration
    /// </summary>
    [XmlElement("DEProcessing")]
    public ProcessConfig DEProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the ContentProcessing configuration
    /// </summary>
    [XmlElement("ContentProcessing")]
    public ProcessConfig ContentProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the ContentFeedProcessing configuration
    /// </summary>
    [XmlElement("ContentFeedProcessing")]
    public ProcessConfig ContentFeedProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the HistoryProcessing configuration
    /// </summary>
    [XmlElement("HistoryProcessing")]
    public ProcessConfig HistoryProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the RulesProcessing configuration
    /// </summary>
    [XmlElement("RulesProcessing")]
    public ProcessConfig RulesProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the ValueListsProcessing configuration
    /// </summary>
    [XmlElement("ValueListsProcessing")]
    public ProcessConfig ValueListsProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the DeleteProcessing configuration
    /// </summary>
    [XmlElement("DeleteProcessing")]
    public ProcessConfig DeleteProcessing
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the FederationProcessing configuration
    /// </summary>
    [XmlElement("FederationProcessing")]
    public ProcessConfig FederationProcessing
    {
      get;
      set;
    }
  }

  /// <summary>
  /// Configuration for the AWS Feed Constants located in ICNETSOA.
  /// </summary>
  [Serializable]
  public class ProcessConfig
  {
    /// <summary>
    /// Gets or sets the name of the Feed Dynamo Table
    /// </summary>
    [XmlElement("DynamoConfig")]
    public DynamoConfig DynamoInfo
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the name of the Feed Queue Table
    /// </summary>
    [XmlElement("QueueName")]
    public string QueueName
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the name of the Feed Queue Poll
    /// </summary>
    [XmlElement("PollIntervalInSeconds")]
    public int PollIntervalInSeconds
    {
      get;
      set;
    }
  }
  /// <summary>
  /// Configuration for the AWS Feed Constants located in ICNETSOA.
  /// </summary>
  [Serializable]
  public class DynamoConfig
  {
    /// <summary>
    /// Gets or sets the name of the Dynamo Table
    /// </summary>
    [XmlElement("TableName")]
    public string TableName
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the name of the Dynamo Table hash key name
    /// </summary>
    [XmlElement("HashName")]
    public string HashName
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the name of the Dynamo Table range key name
    /// </summary>
    [XmlElement("RangeName", IsNullable = true)]
    public string RangeName
    {
      get;
      set;
    }

    [XmlArray("Values", IsNullable = true)]
    [XmlArrayItem("Value")]
    public List<Value> Values
    {
      get;
      set;
    }

  }

  /// <summary>
  /// Configuration for the AWS Feed Constants located in ICNETSOA.
  /// </summary>
  [Serializable]
  public class Value
  {
    /// <summary>
    /// Gets or sets whether this value is a Global Secondary Index
    /// </summary>
    [XmlAttribute(DataType="boolean", AttributeName="IsGSI"), DefaultValue(false)]
    public bool IsGSI
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the name of the Dynamo Table hash key name
    /// </summary>
    [XmlText]
    public string ValueName
    {
      get;
      set;
    }
  }
}
